package com.kaishengit.web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.kaishengit.entity.Document;
import com.kaishengit.entity.UserProject;
import com.kaishengit.service.DocumentService;
import com.kaishengit.util.Page;
import com.kaishengit.util.StringUtil;

public class DocumentServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String method=request.getParameter("m");
		if(StringUtil.isEmpty(method)){
			list(request,response);
		}else if("new".equals(method)){
			toNew(request,response);
		}else if("save".equals(method)){
			save(request,response);	
		}else if("update".equals(method)){
			update(request,response);	
		}else if("del".equals(method)){
			del(request,response);	
		}else if("toupdate".equals(method)){
			toupdate(request,response);	
		}
	}

	private void toupdate(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		DocumentService service=new DocumentService();
		Document document=service.findById(id);
		request.setAttribute("document", document);
		request.getRequestDispatcher("/WEB-INF/views/edit_document.jsp").forward(request, response);
	}

	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id=request.getParameter("id");
		DocumentService service=new DocumentService();
		service.del(id);
		response.sendRedirect("document.jspx");
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id=request.getParameter("id");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		DocumentService ds=new DocumentService();
		ds.update(id,title,content);
		response.sendRedirect("document.jspx");
	}

	private void save(HttpServletRequest request, HttpServletResponse response) throws IOException  {
		HttpSession session=request.getSession();
		UserProject userProject=(UserProject)session.getAttribute("userProject");
		
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		
		DocumentService service=new DocumentService();
		service.save(title,content,userProject);
		
		response.sendRedirect("document.jspx");
	}

	private void toNew(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/views/new_document.jsp").forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String p=request.getParameter("p");
		int pageNum=0;
		if(p==null||"".equals(p)){
			pageNum=1;
		}else{
			try {
				pageNum=Integer.parseInt(p);
			} catch (NumberFormatException e) {
				pageNum=1;
			}
		}
		HttpSession session=request.getSession();
		UserProject up=(UserProject) session.getAttribute("userProject");
		String projectid = up.getProjectId();
		
		DocumentService service=new DocumentService();
	Page<Document> page=service.findAll(pageNum,projectid);
	  request.setAttribute("page", page);
		
		request.getRequestDispatcher("/WEB-INF/views/document.jsp").forward(request, response);
		
	}
	
	
	
	

}
